//
//  DPDetailViewController.m
//  TimeManager
//
//  Created by 刘凯敏 on 01/12/2016.
//  Copyright © 2016 刘凯敏. All rights reserved.
//

#import "AddDPDetailViewController.h"
#import "DPDetailModel.h"

@implementation AddDPDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //添加观察者
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.nameField];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.placeField];
}

-(void) textChange{
    //修改按钮状态
    //    if(self.nameField.text.length && self.pwdField.text.length){
    //        self.loginBtn.enabled = YES;
    //    }
    //    else {
    //        self.loginBtn.enabled = NO;
    //    }
    //等价于
    self.addBtn.enabled = (self.nameField.text.length && self.placeField.text.length);
}

-(void) viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    //让姓名文本框成为第一响应者（叫出键盘）
    [self.nameField becomeFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

//添加数据
- (IBAction)addAction {
    //1.关闭当前视图控制器
    [self.navigationController popViewControllerAnimated:YES];
    //2.代理传值
    //如果协议相应了方法
    if([self.delegate respondsToSelector:@selector(addContact:didiAddContact:)]){
        DPDetailModel *contactModel = [[DPDetailModel alloc] init];
        contactModel.name = self.nameField.text;
        contactModel.place = self.placeField.text;
        [self.delegate addContact:self didiAddContact:contactModel];
    }
}

//触碰背景关闭键盘
- (IBAction)backTap:(id)sender {
    //让nameField控件放弃作为第一响应者
    [self.nameField resignFirstResponder];
    //让pwdField放弃作为第一响应者
    [self.placeField resignFirstResponder];
}

- (IBAction)backAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

//完成输入后关闭键盘
- (IBAction)editEnd:(id)sender {
    //sender放弃作为第一响应者
    [sender resignFirstResponder];
}
@end
